package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.CircleTypeBean;

/**
 * Created by 廉冲-android开发 on 2017/9/19.
 */
public class CircleTypeAdapter extends RecyclerView.Adapter<CircleTypeAdapter.ViewHolder> {
    private List<CircleTypeBean.DataEntity> list;
    private Context context;
    private LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;
    private int selectPosition = -1;
    private List<ViewHolder> viewHolderList = new ArrayList<>();
    int tv_type = -1;

    public CircleTypeAdapter(Context context, List<CircleTypeBean.DataEntity> list){
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void getRefreshList(int id){
        tv_type = id;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView tv = (TextView) inflater.inflate(R.layout.item_circle_type_child, parent, false);
        ViewHolder viewHolder = new ViewHolder(tv);
        viewHolderList.add(viewHolder);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CircleTypeBean.DataEntity bean = list.get(position);
        holder.tv.setText(bean.getType_name());
        if(position == tv_type){
            holder.tv.setBackgroundResource(R.drawable.shape_publish_circle_type_btn_bg_sel);
            holder.tv.setTextColor(context.getResources().getColor(R.color.color_app));
        }else{
            holder.tv.setBackgroundResource(R.drawable.shape_publish_circle_type_btn_bg_null);
            holder.tv.setTextColor(context.getResources().getColor(R.color.color_text_333));
        }
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tv;
        public ViewHolder(View itemView) {
            super(itemView);
            this.tv = (TextView) itemView;
        }
    }
}
